/**
 * Package	: com.coship.adstorm.service.impl
 * File		: PositionBasicInfoServiceImpl.java
 * Company 	: Shenzhen Coship Electronics CO., LTD.
 * Version  : v1.0
 * Description 	: <Detail Description for package>
 *
 * Copyright (c) 2003-2009 Shenzhen Coship Electronics CO., LTD.
 * Rainbow Bldg.,North,Hi-Tech Industrial Park,Nanshan District,Shenzhen,China,P.C.518057.
 * All rights reserved.
 * The contents of this file are confidential and proprietary to COSHIP.
 * Copying is explicitly prohibited without the express permission of COSHIP.
 *
 * Create Date	: Apr 26, 2010
 * Create By    : 904120
 * History :$
 */
package com.coship.vasms.adstorm.service.impl;

import org.apache.commons.lang.StringUtils;

import com.coship.vasms.adstorm.common.DateHelper;
import com.coship.vasms.adstorm.dao.PositionBasicInfoDao;
import com.coship.vasms.adstorm.model.PositionBasicInfo;
import com.coship.vasms.adstorm.service.ADArrangeService;
import com.coship.vasms.adstorm.service.PositionBasicInfoService;
import com.coship.vasms.adstorm.service.PositionInfoService;
import com.coship.vasms.adstorm.webservice.WebServiceConstant;
import com.coship.vasms.adstorm.webservice.position.Response;
import com.coship.vasms.adstorm.webservice.position.positionbasicinfo.vo.ImageSize;
import com.coship.vasms.adstorm.webservice.position.positionbasicinfo.vo.PositionBasicInfoReq;
import com.coship.vasms.adstorm.webservice.position.positionbasicinfo.vo.PositionSize;

/**
 * 
 * File Name : PositionBasicInfoServiceImpl.java
 * 
 * @Description : 广告位基本信息服务接口实现类
 * @author 904120
 */
public class PositionBasicInfoServiceImpl implements PositionBasicInfoService {
	// get set注入
	private PositionBasicInfoDao positionBasicInfoDao;
	// get set注入
	private PositionInfoService positionInfoService;
	private ADArrangeService adArrangeService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.coship.adstorm.service.PositionBasicInfoService#isExistPositionBasicCode(java.lang.String)
	 */
	@Override
	public boolean isExistPositionBasicCode(String basicCode) {
		// TODO Auto-generated method stub
		PositionBasicInfo basicinfo = new PositionBasicInfo();
		basicinfo.setPositionBasicCode(basicCode);
		int count = positionBasicInfoDao
				.countPositionBasicByCondition(basicinfo);
		return count > 0 ? true : false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.coship.adstorm.service.PositionBasicInfoService#isExistPositionBasicName(java.lang.String)
	 */
	@Override
	public boolean isExistPositionBasicName(String name) {
		// TODO Auto-generated method stub
		PositionBasicInfo basicinfo = new PositionBasicInfo();
		basicinfo.setName(name);
		int count = positionBasicInfoDao
				.countPositionBasicByCondition(basicinfo);
		return count > 0 ? true : false;
	}

	/**
	 * @return the positionBasicInfoDao
	 */
	public PositionBasicInfoDao getPositionBasicInfoDao() {
		return positionBasicInfoDao;
	}

	/**
	 * @param positionBasicInfoDao
	 *            the positionBasicInfoDao to set
	 */
	public void setPositionBasicInfoDao(
			PositionBasicInfoDao positionBasicInfoDao) {
		this.positionBasicInfoDao = positionBasicInfoDao;
	}

	/**
	 * 
	 * Description : <Detail Description for method>
	 * 
	 * @param requset
	 * @return
	 * 
	 */
	private PositionBasicInfo buildPositionBasicInfo(
			PositionBasicInfoReq requset) {
		PositionBasicInfo info = new PositionBasicInfo();
		if(!StringUtils.isEmpty(requset.getAgentType())){
			info.setAgentType(Integer.parseInt(requset.getAgentType()));
		}
		
		info.setName(requset.getName());
		info.setPositionBasicCode(requset.getPositionBasicCode());
		if(!StringUtils.isEmpty(requset.getContentType())){
			info.setContentType(Integer.parseInt(requset.getContentType()));
		}		
		// 位置大小
		PositionSize positionSize = requset.getPositionSize();
		if (positionSize != null) {
			if (!StringUtils.isEmpty(positionSize.getCharacterSize())) {
				info.setCharacterSize(Integer.parseInt(positionSize
						.getCharacterSize()));
			}

			ImageSize imageSize = positionSize.getImageSize();
			if (imageSize != null) {
				if (!StringUtils.isEmpty(imageSize.getWidth())) {
					info.setWidth(Integer.parseInt(imageSize.getWidth()));
				}
				if (!StringUtils.isEmpty(imageSize.getHight())) {
					info.setHight(Integer.parseInt(imageSize.getHight()));
				}
			}

		}

		return info;
	}

	@Override
	public Response addBasicInfo(PositionBasicInfo positionBasicInfo) {
		// TODO Auto-generated method stub
		Response response = null;
		// 1.基本位信息:编码是否重复
		boolean existed = isExistPositionBasicCode(positionBasicInfo
				.getPositionBasicCode());
		if (existed) {
			response = new Response(
					WebServiceConstant.RESPONSE_ERROR_BASICCODE, "广告位基本信息编码已存在");
			return response;
		}
		// 2.基本位信息:名称是否重复
		existed = isExistPositionBasicName(positionBasicInfo.getName());
		if (existed) {
			response = new Response(
					WebServiceConstant.RESPONSE_ERROR_BASICNAME,
					"广告位基本信息名称已存在");
			return response;
		}
		positionBasicInfo.setCreateTime(DateHelper.getCurrentSecond());//设置创建时间
		positionBasicInfoDao.insertPositionBasicInfo(positionBasicInfo);
		response = new Response(WebServiceConstant.RESPONSE_SUCCESS, null);
		return response;
	}

	@Override
	public Response deleteBasicInfo(PositionBasicInfo positionBasicInfo) {
		// TODO Auto-generated method stub
		// 1.先更新广告位附加数据状态
		positionInfoService.updatePositionInfoState(positionBasicInfo
				.getPositionBasicCode(),
				WebServiceConstant.POSITIONINFO_STATE_N);		
		// 2.更新排期数据状态为无效
		adArrangeService.updateArrangeByBasicCode(positionBasicInfo
				.getPositionBasicCode(), WebServiceConstant.ARRANGE_STATE_N);
		// 3.更新状态为无效
		positionBasicInfo.setState(WebServiceConstant.POSITIONINFO_STATE_N);
		positionBasicInfo.setLastUpdateTime(DateHelper.getCurrentSecond());//设置更新时间
		positionBasicInfoDao.updatePositionBasicInfo(positionBasicInfo);
		Response response = new Response(WebServiceConstant.RESPONSE_SUCCESS, null);
		return response;
	}

	@Override
	public Response modifyBasicInfo(PositionBasicInfo positionBasicInfo) {
		// TODO Auto-generated method stub
		Response response = null;
		// 1.基本位信息:编码是否重复
		boolean existed = isExistPositionBasicCode(positionBasicInfo
				.getPositionBasicCode());
		if (!existed) {
			response = new Response(
					WebServiceConstant.RESPONSE_ERROR_BASICCODE, "广告位基本信息编码不存在");
			return response;
		}
		// 1.先更新广告位附加数据状态为无效
		positionInfoService.updatePositionInfoState(positionBasicInfo
				.getPositionBasicCode(),
				WebServiceConstant.POSITIONINFO_STATE_N);
		// 2.更新排期数据状态为无效
		adArrangeService.updateArrangeByBasicCode(positionBasicInfo
				.getPositionBasicCode(), WebServiceConstant.ARRANGE_STATE_N);
		// 3.更新广告位基本信息为无效
		positionBasicInfo.setState(WebServiceConstant.POSITIONINFO_STATE_N);
		positionBasicInfo.setLastUpdateTime(DateHelper.getCurrentSecond());//设置更新时间
		positionBasicInfoDao.updatePositionBasicInfo(positionBasicInfo);
		response = new Response(WebServiceConstant.RESPONSE_SUCCESS, null);
		return response;
	}

	@Override
	public Response synBasicInfo(PositionBasicInfoReq requset) {
		// TODO Auto-generated method stub
		PositionBasicInfo positionBasicInfo = buildPositionBasicInfo(requset);
		if (positionBasicInfo != null) {			
			if (WebServiceConstant.POSITIONBASICINFO_OP_ADD
					.equalsIgnoreCase(requset.getOperation())) {// 新增
				return addBasicInfo(positionBasicInfo);
			
			} else if (WebServiceConstant.POSITIONBASICINFO_OP_MODIFY
					.equalsIgnoreCase(requset.getOperation())) {// 修改
				return modifyBasicInfo(positionBasicInfo);
				
			} else if (WebServiceConstant.POSITIONBASICINFO_OP_DELETE
					.equalsIgnoreCase(requset.getOperation())) {// 删除
				return deleteBasicInfo(positionBasicInfo);
				
			} else {
				throw new RuntimeException("不支持的对广告位基本信息操作");
			}
		}
		return null;
	}

	/**
	 * @return the positionInfoService
	 */
	public PositionInfoService getPositionInfoService() {
		return positionInfoService;
	}

	/**
	 * @param positionInfoService
	 *            the positionInfoService to set
	 */
	public void setPositionInfoService(PositionInfoService positionInfoService) {
		this.positionInfoService = positionInfoService;
	}

	/**
	 * @return the adArrangeService
	 */
	public ADArrangeService getAdArrangeService() {
		return adArrangeService;
	}

	/**
	 * @param adArrangeService
	 *            the adArrangeService to set
	 */
	public void setAdArrangeService(ADArrangeService adArrangeService) {
		this.adArrangeService = adArrangeService;
	}

}
